package org.eredlab.g4.lz.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eredlab.g4.arm.util.idgenerator.IDHelper;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.ccl.util.G4Constants;
import org.eredlab.g4.ccl.util.G4Utils;
import org.eredlab.g4.lz.service.OrderService;
import org.eredlab.g4.rif.util.SessionContainer;
import org.eredlab.g4.rif.util.WebUtils;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

public class OrdersAction extends BaseAction{
	
	/**
	 * service
	 */
	private OrderService orderservice = (OrderService)getService("OrderService");
	
	/**
	 * 商品初始查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward orderInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("orderInit");
	}
	
	/**
	 * 查询所有商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryGoods(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List areaList = g4Reader.queryForList("lz.getgoodData", dto);
		String jsonString = JsonHelper.encodeObject2Json(areaList);
		write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/**
	 * 查询所有商品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryGood(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		Dto outDto = (Dto) g4Reader.queryForObject("lz.loadgoodData", dto);
		String jsonString = JsonHelper.encodeObject2Json(outDto);
		write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/**
	 * 商品初始查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward detailInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("detailInit");
	}
	
	/**
	 * 查询订单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryOrders(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("lz.order_select_1", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("lz.order_select_1_count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger, G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/**
	 * 查询订单明细
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForList("lz.detail_select_1", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("lz.good_select_1_count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger, G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	/**
	 * 新增订单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		SessionContainer sessionContainer = WebUtils.getSessionContainer(request);
		Dto inDto = aForm.getParamAsDto(request);
		inDto.put("updatetime", G4Utils.getCurrentTimestamp());//建立时间  当前时间
		inDto.put("opernames", sessionContainer.getUserInfo().getUserid());//操作人姓名
		//更新
		if(!"".endsWith(inDto.getAsString("ordernos"))){
			String dels = inDto.getAsString("deldata");
			String del = dels.replace("[", "").replace("]", "").replace("\"", "").trim();
			System.out.println(dels);
			//------子表数据
			List list  = aForm.getGridData(request,"detailData");
			Map<String,BaseDto> saveMap = new HashMap<String,BaseDto>();
			//字表数据
			for (int i = 0; i < list.size(); i++) {
				BaseDto dto = (BaseDto)list.get(i);
				String goodno = dto.getAsString("goodno");				
				if(saveMap.get(goodno) == null){
					saveMap.put(goodno, dto);
				}else{
					BaseDto n = saveMap.get(goodno);
					n.put("ordernum",n.getAsInteger("ordernum") + dto.getAsInteger("ordernum"));
					saveMap.put(goodno, n);
				}
			}
			orderservice.updateOrder(inDto, new ArrayList<Dto>(saveMap.values()), del);
		}else{
			//新增
			//生产时间当前时间
			inDto.put("createtime", G4Utils.getCurrentTimestamp());//建立时间  当前时间
			inDto.put("operStatus", 0);//建立时间  当前时间  originsource/orders
			inDto.put("orders", sessionContainer.getUserInfo().getDeptid());
			inDto.put("isdel", 0);//操作人姓名
			//------子表数据
			List list  = aForm.getGridData(request,"detailData");
			Map<String,BaseDto> saveMap = new HashMap<String,BaseDto>();
			//字表数据
			for (int i = 0; i < list.size(); i++) {
				BaseDto dto = (BaseDto)list.get(i);
				String goodno = dto.getAsString("goodno");
				//查询goodno对应的数据价格，优惠幅度
				Dto pdto = (Dto)g4Reader.queryForObject("lz.good_select_1_price", dto);
				if(pdto == null){
					setOkTipMsg("数据保存失败", response);
					super.write(inDto.toJson(), response);
					return mapping.findForward(null);
				}
				dto.put("dj", pdto.getAsString("price"));
				dto.put("zk", pdto.getAsString("discount"));
				if(saveMap.get(goodno) == null){
					saveMap.put(goodno, dto);
				}else{
					BaseDto n = saveMap.get(goodno);
					n.put("ordernum",n.getAsInteger("ordernum") + dto.getAsInteger("ordernum"));
					saveMap.put(goodno, n);
				}
			}
			
			orderservice.insertOrder(inDto, new ArrayList<Dto>(saveMap.values()));
		}

		inDto.put("result", "数据保存成功");
		super.write(inDto.toJson(), response);
		return mapping.findForward(null);
	}
	
	/**
	 * 更改订单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("lz.order_select_1", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("lz.good_select_1_count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger, G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/**
	 * 操作
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward operOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		SessionContainer sessionContainer = WebUtils.getSessionContainer(request);
		//Dto dto = aForm.getParamAsDto(request);
		Dto inDto = aForm.getParamAsDto(request);
		//------主表数据
		//生产时间当前时间
		inDto.put("updatetime", G4Utils.getCurrentTimestamp());//建立时间  当前时间
		inDto.put("opernames", sessionContainer.getUserInfo().getUserid());//操作人姓名
		int type = inDto.getAsInteger("operType");
		int recount = 0;
		switch (type) {
		case 1://学校审核
			inDto.put("operstatus", "1");//建立时间  当前时间
			recount = orderservice.auditOrderBySchool(inDto);
			break;
		case 2://驳回
			inDto.put("operstatus", "2");//建立时间  当前时间
			recount = orderservice.auitOrderByReject(inDto);
			break;
		case 3://通过
			inDto.put("operstatus", "3");//建立时间  当前时间
			List<Dto> pstock = g4Reader.queryForList("lz.perstock", inDto);
			for(Dto dto : pstock){
				dto.put("stockno", IDHelper.getGoodId("QC", 0));//建立时间  当前时间
				dto.put("createTime", G4Utils.getCurrentTimestamp());//建立时间  当前时间
				dto.put("opernames", sessionContainer.getUserInfo().getUserid());//操作人姓名
				dto.put("stockTypes", 0);//操作人姓名
			}
			recount = orderservice.auitOrderByBoard(inDto,pstock);
			break;
		case 4://删除
			inDto.put("isdel", "1");//删除
			recount = orderservice.deleteOrder(inDto);
			break;
		default:
			break;
		}
		
		Dto re=  new BaseDto();
		if(recount >0 ){
			re.put("result", "操作成功");
		}else{
			re.put("result", "操作失败");
		}
		super.write(re.toJson(), response);
		return mapping.findForward(null);
	}

}
